package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.event.SelectEvent;
import com.mdlib.helper.model.entity.SelectEntity;
import com.mdlib.helper.ui.adapter.SelectAdapter;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/12/8 15:19
 * 邮箱：hqy_xz@126.com
 */

public class SelectDialgoFragment extends DialogFragment {


    private static final String LIST = "list";
    private static final String TYPE = "type";
    @Bind(R.id.lv_personal_lsit)
    RecyclerView mRvPersonalLsit;
    @Bind(R.id.rl_window_cancel)
    RelativeLayout mRlWindowCancel;


    private List<SelectEntity> selectList = new ArrayList<>();

    private SelectAdapter mSelectAdapter;
    private String type;


    public static SelectDialgoFragment newInstance(List<SelectEntity> list, String type) {
        SelectDialgoFragment fragment = new SelectDialgoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(LIST, (Serializable) list);
        bundle.putString(TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            selectList = (List<SelectEntity>) bundle.getSerializable(LIST);
            type = bundle.getString(TYPE);
        }

    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_select_list, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        getList();
        return builder.create();
    }

    private void getList() {
        mSelectAdapter = new SelectAdapter(selectList);
        mRvPersonalLsit.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvPersonalLsit.setAdapter(mSelectAdapter);
        mRvPersonalLsit.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                EventBus.getDefault().post(new SelectEvent(selectList.get(position), type));
                dismiss();
            }
        });
    }


    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick(R.id.rl_window_cancel)
    public void onClick() {
        dismiss();
    }
}
